import time

import pytest

from pythoncode.calculator import Calculator
from pythontestcode.tools.tool import get_data


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


@pytest.fixture(autouse=True)
def print_start():
    print("开始计算")
    yield
    print("结束计算")


@pytest.fixture(scope="session", autouse=True)
def print_end():
    cal = Calculator()
    yield cal
    print("结束测试")


@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d %H-%M-%S")
    log_name = './logs/' + now + '.logs'

    request.config.pluginmanager.get_plugin("logging-plugin") \
        .set_log_path(log_name)


path = './datas/count.yml'


@pytest.fixture(params=get_data(path=path, cal='add', num='P0')[0], ids=get_data(path=path, cal='add', num='P0')[1])
def get_P0_data(request):
    return request.param


@pytest.fixture(params=get_data(path=path, cal='add', num='P1_1')[0], ids=get_data(path=path, cal='add', num='P1_1')[1])
def get_P1_1_data(request):
    return request.param


@pytest.fixture(params=get_data(path=path, cal='add', num='P1_2')[0], ids=get_data(path=path, cal='add', num='P1_2')[1])
def get_P1_2_data(request):
    return request.param


@pytest.fixture(params=get_data(path=path, cal='add', num='P2')[0], ids=get_data(path=path, cal='add', num='P2')[1])
def get_P2_data(request):
    return request.param